<template>
  <div>
    <div>
      <div class="e-flex e-col-center box">
        <div class="e-m-r-20 e-font-10 e-font-bold">创建时间</div>
        <div :span="21">
          <div class="e-flex">
            <div v-for="(item,index) in time" :key="index" class="block" @click="select(item,'time',index)"
              :class="item.ischange?'change':''">
              {{item.name}}
            </div>
          </div>
        </div>
      </div>

      <div class="e-flex e-col-center box">
        <div class="e-m-r-20 e-font-10 e-font-bold">客户状态</div>
        <div :span="21">
          <div class="e-flex">
            <div v-for="(item,index) in status" :key="index" class="block" @click="select(item,'status',index)"
              :class="item.ischange?'change':''">
              {{item.name}}
            </div>
          </div>
        </div>
      </div>

      <div class="e-flex e-col-center box">
        <div class="e-m-r-20 e-font-10 e-font-bold">归属销售</div>
        <div :span="21">
          <div class="e-flex">

            <div v-for="(item,index) in belong" :key="index" class="block u-line-1" @click="select(item,'belong',index)"
              :class="item.ischange?'change':''">
              <el-tooltip v-if="item.name.length>6" class="item" effect="dark" :content="item.name"
                placement="top-start">
                <span>{{item.name}}</span>
              </el-tooltip>
              <span v-else>{{item.name}}</span>
            </div>
          </div>
        </div>
      </div>

      <div class="e-flex e-col-center box">
        <div class="e-m-r-20 e-font-10 e-font-bold">项目类型</div>
        <div :span="21">
          <div class="e-flex">
            <div v-for="(item,index) in type" :key="index" class="block" @click="select(item,'type',index)"
              :class="item.ischange?'change':''">
              {{item.name}}
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- table -->
    <div style="background: #fff;">

      <div class="  e-p-20  e-flex e-row-between">
        <div class="e-flex">

          <div style="width: 300px;margin-right: 20px;">
            <el-input v-model="initial.params.keyword" placeholder="搜索客户手机号/姓名"></el-input>
          </div>
          
          <div style="width: 300px;margin-right: 20px;">
            <el-input v-model="initial.params.follow" placeholder="搜索跟进关键词"></el-input>
          </div>
          
          <el-button type="primary" @click="keyword">搜索</el-button>
        </div>
        <div>
          <el-button type="success" icon="el-icon-edit" @click="createdUser">创建客户</el-button>
        </div>
      </div>

      <el-table :data="list.data" stripe style="width: 100%" height="500"  @sort-change="sortChange">
        <!-- <el-table-column fixed prop="id" label="客户编号" >
        </el-table-column> -->
        <el-table-column prop="realname" label="客户姓名">
          <template slot-scope="scop">
            <div @click="flow(scop.row)">{{scop.row.realname}}</div>
            </el-rate>
          </template>
        </el-table-column>
        <el-table-column prop="star" label="跟进状态" width="150">
          <template slot-scope="scop">
            <el-rate v-model="scop.row.star" disabled text-color="#ff9900">
            </el-rate>
          </template>
        </el-table-column>
        <el-table-column prop="phone" label="手机号" width="150">
        </el-table-column>
        <el-table-column prop="type" label="项目类型">
        </el-table-column>
        <el-table-column prop="lastFollowTime" label="跟进时间" sortable='custom'   width="200">
        </el-table-column>
        <el-table-column prop="createTime" label="录入时间" width="200" sortable='custom'    >
        </el-table-column>
        <el-table-column prop="flow" label="流转">
        </el-table-column>
        <el-table-column prop="company" label="公司名称 ">
        </el-table-column>
        <el-table-column prop="staffName" label="跟进人">
        </el-table-column>
        <el-table-column prop="fromWay" label="客户来源 ">
        </el-table-column>
        <el-table-column label="操作" :width="role>2?180:100">
          <template slot-scope="scop">
            <div style="display: flex;">
              <!-- <el-button size="mini" @click="open(scop.row)" type="primary" icon="el-icon-edit">操作</el-button> -->
              <el-button size="mini" @click="flow(scop.row)" type="success" icon="el-icon-user">跟进</el-button>
              <el-button v-if="role>2" size="mini" @click="transfer(scop.row)" type="danger" icon="el-icon-refresh">流转
              </el-button>
            </div>
          </template>
        </el-table-column>

      </el-table>
    </div>

    <div style="display: flex;justify-content: flex-end;margin: 20px 0;">
      <basePagination :size="list.perPage" :page="list.currentPage" :total="list.total" @SizeChange='SizeChange'
        @CurrentChange='CurrentChange' />
    </div>

    <flow ref="flowChild" @update="update"></flow>

    <createdUser ref="userChild" @update="update"></createdUser>
  </div>
</template>

<script>
  import {
    Message,
    MessageBox
  } from 'element-ui';
  import loader from '@/components/base/basePagination/controls.js';
  import {
    typeArry,
    statusArry,
    timeArry,

  } from '@/common/arry.js';
  import createdUser from '../../components/createdUser.vue';
  import flow from '../../components/flow.vue';
  export default {
    mixins: [loader],
    components: {
      createdUser,
      flow
    },
    data() {
      return {
        initial: {
          geturl: "/user/clientList",
          params: {
            list: 1,
            keyword: "",
            date: '',
            star: '',
            staff_id: '',
            type: '',
            sort:'create_time desc',
            follow:''
          }
        },
        list: {},
        loading: true,
        input: '',
        time: JSON.parse(JSON.stringify(timeArry)),
        status: JSON.parse(JSON.stringify(statusArry)),
        belong: [],
        type: JSON.parse(JSON.stringify(typeArry)),
        transId: '',
        role: ''
      }
    },
    created() {
      this.information()
      this.jiazai()
      this.belongTo();
      this.$forceUpdate()
    },

    methods: {
      sortChange(e){

        if(e.prop=='createTime'){
           if(e.order=='descending'){
             this.initial.params.sort='create_time desc'
           }else{
             this.initial.params.sort='create_time asc'
           }

        }

        if(e.prop=='lastFollowTime'){
           if(e.order=='descending'){
             this.initial.params.sort='last_follow_time desc'
           }else{
             this.initial.params.sort='last_follow_time asc'
           }

        }
        this.update()

      },
      information() {
        this.axios.post("/user/information", {}).then(res => {
          this.role = res.role
        })
      },
      transfer(item) {
        MessageBox.confirm('此用户将要流转, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.post("/user/transfer", {
            id: item.id
          }).then(res => {
            Message.success({
              message: "成功",
              onClose: (res) => {
                this.jiazai()
              }
            })
          })
        }).catch(() => {});
      },
      keyword() {
        this.initial.page=1;

        this.jiazai()
      },
      jiazai() {
        this.getData(this.initial.params).then(res => {
          this.list = res;
          this.loading = false;
        }).catch(err => {

        })
      },
      update() {
        this.initial.page=1;
        this.jiazai()
      },
      createdUser() {
        this.$refs.userChild.dialogVisible = true;

      },

      select(item, type, index) {
        this[type].forEach((item, index1) => {
          if (index == index1) {
            if (this[type][index].ischange) {
              this.$set(this[type][index1], 'ischange', false);
            } else {
              this.$set(this[type][index], 'ischange', true);
            }
          } else {
            this.$set(this[type][index1], 'ischange', false)
          }
        })

        this.lookquery(type)
      },
      lookquery(type) {
        var arr = this[type].filter(item => item.ischange == true);
        if (type == 'time') {
          if (arr.length) {
            this.initial.params.date = arr[0].id
          } else {
            this.initial.params.date = ''
          }
        }
        if (type == 'status') {
          if (arr.length) {
            this.initial.params.star = arr[0].id
          } else {
            this.initial.params.star = ''
          }
        }
        if (type == 'belong') {
          if (arr.length) {
            this.initial.params.staff_id = arr[0].id
          } else {
            this.initial.params.staff_id = ''
          }
        }
        if (type == 'type') {
          if (arr.length) {
            this.initial.params.type = arr[0].id
          } else {
            this.initial.params.type = ''
          }
        }
        this.jiazai()
      },
      belongTo() {
        this.axios.post("/user/belongTo").then(res => {
          if (res.department.length) {
            this.belong = [...this.belong, ...res.staff];
          } else {
            this.belong = res.staff;
          }
        }).catch(error => {

        })
      },
      open(item) {
        this.$refs.userChild.dialogVisible = true;
        this.$refs.userChild.echo(item)
      },
      flow(item) {
        let id = item.id;
        this.$refs.flowChild.dialogVisible=true;
        this.$refs.flowChild.jiazai(id);
        this.$refs.flowChild.echo(id);
      },


    }
  }
</script>

<style scoped>
  .block {
    background: #fff;
    margin-right: 30px;
    border-radius: 2px;
    font-size: 9px;
    width: 100px;
    text-align: center;
    text-align: center;
    padding: 10px;
    color: #333;
  }

  .change {
    background: #0076D3;
    color: #fff;
    transition: all 0.3s;
  }

  .box {
    margin-bottom: 10px;
  }

  /deep/ .el-textarea__inner {
   height: 200px;
  }

</style>
